<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Students Model
*
* Author:  Ervin R. Musngi
*
*/

class School_year_m extends DataMapperExt
{
	var $has_many	= array(
							'students'			=> array(
								'class'			=> 'student_m',
								'other_field'	=> 'school_year',
								'join_other_as'	=> 'student',
								'join_table'	=> 'student_listings'
							),
							'sections'			=> array(
								'class'			=> 'section_m',
								'other_field'	=> 'school_years',
								'join_self_as'	=> 'school_year',
								'join_other_as'	=> 'section',
								'join_table'	=> 'section_listings'
							),
							'section_listings'	=> array(
								'class'			=> 'section_listing_m',
								'other_field'	=> 'school_years',
								'join_self_as'	=> 'school_year',
								'join_table'	=> 'section_listings'
							)
					);	
	
	var $has_one	= array();
	
	var $table = 'school_years';
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function lock_school_year( $id )
	{
	
		$school_year = new School_year_m();
		
		$school_year->where('id',$id)->get();
		$school_year->section_listings->get();
		
		if ( $school_year->section_listings->result_count() == 0 )
		{
			return false;
		}
		
		$data 		= array();
		foreach ( $school_year->section_listings as $s )
		{
			
			if ( $s->students->get()->result_count() == 0 || $s->school_levels->get()->subject_listings->get()->result_count() == 0  )
			{
				$data[] = $s->section_name;
			}
			

		}
		
		if ( count($data) > 0 )
		{
			return $data;
		}

		$school_year->where('id',$id)->update(array('is_lock'=> 1));

		return true;
	}
	
	public function create_new_school_year()
	{
		$school_y = new School_year_m();
		
		if ( $school_y->where('is_lock',0)->get()->result_count() > 0 )
		{
			$data = array(
				'msg_title'	=> lang('school_year_title_error'),
				'msg'		=> lang('school_year_existing_year')
			);
			
			$this->session->set_flashdata($data);
			return false;
		}
		
		$sections = new Section_m();
		
		if ( $sections->where('is_active',1)->get()->result_count() == 0)
		{
			$data = array(
				'msg_title'	=> lang('school_year_title_error'),
				'msg'		=> lang('school_year_create_no_sections')
			);
			
			$this->session->set_flashdata($data);
			
			return false;
		}
		
		$school_level = new School_level_m();

		//if ( $school_level->where('is_active',1)->where_not_in_related('subjects','school_level_id')->get()->result_count() > 0 )
		//{
		//	$data = array(
		//		'msg_title'	=> lang('school_year_title_error'),
		//		'msg'		=> lang('school_year_no_subject')
		//	);
		//	
		//	$this->session->set_flashdata($data);
		//	
		//	return false;
		//}
		
		foreach ( $school_level->where('is_active',1)->get()  as $s)
		{
			if ( $s->subjects->get()->result_count() == 0 )
			{
				$data = array(
					'msg_title'	=> lang('school_year_title_error'),
					'msg'		=> lang('school_year_no_subject')
				);
				
				$this->session->set_flashdata($data);
				
				return false;
			}
		}
	
		
		$school_year 	= new School_year_m();
		$sy 			= new School_year_m();
		
		$school_year->order_by('id','desc')->get(1);
		
		if ( $school_year->result_count() == 0)
		{
			$year_start = date("Y");
		}
		else
		{
			$year_start = $school_year->year + 1;
		}
		
		$sy->year = $year_start;
		
		$sy->save();
		
		$sections = new Section_m();
		
		$subjects = new Subjects_m();
		
		$sections->where('is_active',1)->include_related('school_level',array('id','name'),TRUE,TRUE)->get();
		
		$subjects->where('is_active',1)
					->include_related('school_level',array('id','name'),TRUE,TRUE)
					->include_related('category',array('id','name'),TRUE,TRUE)->get();
		
		foreach ( $sections as $s )
		{
			$data = array(
				'school_year_id'	=> $sy->id,
				'section_id'		=> $s->id,
				'school_level_id'	=> $s->school_level->id,
				'section_name'		=> $s->section
			);
			
			$this->db->insert('section_listings',$data);
		}
		
		foreach ( $subjects as $s )
		{
			$data = array(
				'school_year_id'		=> $sy->id,
				'subject_id'			=> $s->id,
				'school_level_id'		=> $s->school_level->id,
				'subject_name'			=> $s->name,
				'subject_description'	=> $s->description,
				'subject_code'			=> $s->code,
				'category_id'			=> $s->category->id,
				'category_name'			=> $s->category->name
				
			);
			
			$this->db->insert('subject_listings',$data);
		}
		
		return true;
		
	}
	
	public function get_school_year_id($id_year)
	{
		$school_year = new School_year_m();
		
		$school_year->where('id',$id_year)->or_where('year',$id_year)->get()->section_listings->order_by_related('school_levels','name','ASC')->order_by('section_name','ASC')->get();
		
		return $school_year;
	}
	
	public function get_school_year($limit=10,$offset=0)
	{
		$school_year = new School_year_m();
		
		$school_year->get($limit,$offset);
		
		return $school_year;
	}
	
}